package vn.orderonline.com.core.business.food;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vn.orderonline.com.core.business.dto.FoodDto;
import vn.orderonline.core.commons.utils.DateUtils;
import vn.orderonline.core.commons.utils.StringUtils;
import vn.orderonline.core.dao.AdminDao;
import vn.orderonline.core.dao.FoodDao;
import vn.orderonline.core.dao.MenuFoodFoodDao;
import vn.orderonline.core.dao.entity.Food;
import vn.orderonline.core.dao.entity.MenuFoodFood;
import vn.orderonline.core.dao.entity.MenuFoodFoodId;

@Service
@Transactional
public class FoodImpl implements IFood {

	@Autowired
	private FoodDao foodDao;
	@Autowired
	private MenuFoodFoodDao menuFoodFoodDao;
	@Autowired
	private AdminDao adminDao;

	@Override
	public Boolean checkExistedFood(Integer menuFoodId, String foodName) {
		if (menuFoodId != null && !StringUtils.isNullOrEmpty(foodName)) {
			if (foodDao.checkExist(menuFoodId, foodName)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public void addFood(FoodDto food) {
		Food fo = null;
		MenuFoodFood mff = null;
		if (food != null) {
			fo = new Food();
			fo.setFoodName(food.getFoodName());
			fo.setFoodTitle(food.getFoodTitle());
			fo.setFoodImage(food.getFoodImage());
			fo.setOldPrice(food.getOldPrice());
			fo.setNewPrice(food.getNewPrice());
			fo.setDescription(food.getDescription());
			fo.setFoodStatus(true);
			fo.setCreateTime(new Timestamp(DateUtils.now()));
			fo.setCreateId(food.getCreateId());
			foodDao.save(fo);

			mff = new MenuFoodFood(new MenuFoodFoodId(food.getMenuFoodId(),
					fo.getFoodId()));
			menuFoodFoodDao.save(mff);
		}

	}

	@Override
	public List<FoodDto> getAllFood(Integer resId, Integer menuFoodId) {
		List<FoodDto> list = new ArrayList<>();
		if (resId != null) {
			FoodDto food = null;
			List<Object[]> listObj = foodDao
					.getListFoodByRestaurantIdAndMenuFoodId(resId, menuFoodId);
			for (Object[] obj : listObj) {
				food = new FoodDto();
				food.setRestaurantId((Integer) obj[0]);
				food.setRestaurantName((String) obj[1]);
				food.setMenuFoodId((Integer) obj[2]);
				food.setMenuFoodName((String) obj[3]);
				Food fo = (Food) obj[4];
				food.setFoodId(fo.getFoodId());
				food.setFoodName(fo.getFoodName());
				food.setFoodTitle(fo.getFoodTitle());
				food.setOldPrice(fo.getOldPrice());
				food.setNewPrice(fo.getNewPrice());
				food.setFoodImage(fo.getFoodImage());
				food.setFoodStatus(fo.getFoodStatus());
				food.setDescription(fo.getDescription());
				food.setCreateTime(fo.getCreateTime());
				food.setUpdateTime(fo.getUpdateTime());
				food.setCreateId(fo.getCreateId());
				food.setUpdateId(fo.getUpdateId());
				list.add(food);
			}
		}
		return list;
	}

	@Override
	public FoodDto viewFood(Integer foodId) {
		FoodDto food = null;
		if (foodId != null) {
			Object[] obj = foodDao.getFood(foodId);
			food = new FoodDto();
			food.setRestaurantId((Integer) obj[0]);
			food.setRestaurantName((String) obj[1]);
			food.setMenuFoodId((Integer) obj[2]);
			food.setMenuFoodName((String) obj[3]);
			Food fo = (Food) obj[4];
			food.setFoodId(fo.getFoodId());
			food.setFoodName(fo.getFoodName());
			food.setFoodTitle(fo.getFoodTitle());
			food.setOldPrice(fo.getOldPrice());
			food.setNewPrice(fo.getNewPrice());
			food.setFoodImage(fo.getFoodImage());
			food.setFoodStatus(fo.getFoodStatus());
			food.setDescription(fo.getDescription());
			food.setCreateTime(fo.getCreateTime());
			food.setUpdateTime(fo.getUpdateTime());
			if (!StringUtils.isNullOrEmpty(fo.getCreateId())) {
				String adminName = adminDao.getAdminNameByAccountName(fo.getCreateId());
				if (!StringUtils.isNullOrEmpty(adminName)) {
					food.setCreateId(adminName);
				} else {
					food.setCreateId(food.getCreateId());
				}
			}
			if (!StringUtils.isNullOrEmpty(fo.getUpdateId())) {
				String adminName = adminDao.getAdminNameByAccountName(fo.getUpdateId());
				if (!StringUtils.isNullOrEmpty(adminName)) {
					food.setUpdateId(adminName);
				} else {
					food.setUpdateId(food.getUpdateId());
				}
			}
		}
		return food;
	}

	@Override
	public void updateFood(FoodDto dto) {
		if (dto != null) {
			Food food = foodDao.find(dto.getFoodId());
			food.setFoodTitle(dto.getFoodTitle());
			food.setFoodImage(dto.getFoodImage());
			food.setOldPrice(dto.getOldPrice());
			food.setNewPrice(dto.getNewPrice());
			food.setDescription(dto.getDescription());
			food.setUpdateTime(new Timestamp(DateUtils.now()));
			food.setUpdateId(dto.getUpdateId());
			foodDao.save(food);
		}
	}

	@Override
	public void deleteFood(Integer menuFoodId, Integer foodId) {
		if (menuFoodId != null && foodId != null) {
			MenuFoodFood mff = menuFoodFoodDao.find(new MenuFoodFoodId(
					menuFoodId, foodId));
			menuFoodFoodDao.delete(mff);
			Food food = foodDao.find(foodId);
			foodDao.delete(food);
		}
	}

	@Override
	public List<FoodDto> searchFood(FoodDto food) {
		List<FoodDto> list = new ArrayList<>();
		if (food != null) {
			FoodDto dto = null;
			List<Object[]> listObj = foodDao.searchFood(food.getRestaurantId(), food.getFoodName(),
					food.getFoodTitle(), food.getOldPrice(), food.getNewPrice());
			for (Object[] obj : listObj) {
				dto = new FoodDto();
				dto.setRestaurantId((Integer) obj[0]);
				dto.setRestaurantName((String) obj[1]);
				dto.setMenuFoodId((Integer) obj[2]);
				dto.setMenuFoodName((String) obj[3]);
				Food fo = (Food) obj[4];
				dto.setFoodId(fo.getFoodId());
				dto.setFoodName(fo.getFoodName());
				dto.setFoodTitle(fo.getFoodTitle());
				dto.setOldPrice(fo.getOldPrice());
				dto.setNewPrice(fo.getNewPrice());
				dto.setFoodImage(fo.getFoodImage());
				dto.setFoodStatus(fo.getFoodStatus());
				dto.setDescription(fo.getDescription());
				dto.setCreateTime(fo.getCreateTime());
				dto.setUpdateTime(fo.getUpdateTime());
				dto.setCreateId(fo.getCreateId());
				dto.setUpdateId(fo.getUpdateId());
				list.add(dto);
			}
		}
		return list;
	}

}
